package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Checkin;
import cn.zyy.hotel.entity.Employee;
import cn.zyy.hotel.service.CheckinService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.CheckinVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/checkin")
public class CheckinAdminController {

    @Resource
    private CheckinService checkinService;

    //查询入住列表
    @RequiresPermissions("checkin:view")
    @RequestMapping("/list")
    public Result list(CheckinVO checkinVO){
        //设置分页信息
        PageHelper.startPage(checkinVO.getPage(),checkinVO.getLimit());
        //调用查询入住列表方法
        List<Checkin> checkinList = checkinService.selectCheckinList(checkinVO);
        //创建分页对象
        PageInfo<Checkin> pageInfo = new PageInfo<Checkin>(checkinList);
        //返回数据
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //登记入住
    @RequiresPermissions("checkin:add")
    @RequestMapping("/addCheckin")
    public Object addCheckin(Checkin checkin, HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        //获取当前登录用户
        Employee employee = (Employee)session.getAttribute(SystemConstant.LOGINUSER);
        //创建人
        checkin.setCreatedBy(employee.getId());
        //调用添加入住信息的方法
        if (checkinService.addCheckin(checkin)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"办理入住成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"办理入住失败");
        }
        return map;
    }


}
